<script setup lang="ts">
import { RouterLink, RouterView } from 'vue-router'
import { useRouter } from 'vue-router'
import { ref } from 'vue'
// @ts-ignore
import { userStore } from './stores/user'
// @ts-ignore
import { authAPI } from './services/api'
// 引入Element Plus
import { ElDropdown, ElDropdownMenu, ElDropdownItem, ElDivider } from 'element-plus'

const router = useRouter()

// 添加全局状态来跟踪当前路由
const currentRoute = ref(router.currentRoute.value.path)

// 登录弹窗控制（如果有弹窗登录可以用）
const showLogin = ref(false)

// 自动同步登录状态（刷新后保持登录）
if (localStorage.getItem('token')) {
  userStore.isLogin = true
  userStore.username = localStorage.getItem('username') || ''
}

// 退出登录方法
function logout() {
  authAPI.logout()
  userStore.isLogin = false
  userStore.username = ''
  // 清除所有相关的本地存储
  localStorage.removeItem('token')
  localStorage.removeItem('username')
  localStorage.removeItem('loginId')
  
  // 导航到登录页面
  router.push('/login')
  
  // 添加一个小延迟后刷新页面，确保状态完全重置
  setTimeout(() => {
    window.location.reload()
  }, 100)
}

function handleUserInfoClick() {
  console.log('User info clicked')
}
</script>

<template>
  <div class="app">
    <!-- 完全移除navbar，避免与AIChat组件的header冲突 -->
    
    
    <router-view v-slot="{ Component }">
      <transition name="fade" mode="out-in">
        <component :is="Component" />
      </transition>
    </router-view>
  </div>
</template>

<style lang="scss">
:root {
  --bg-color: #f5f5f5;
  --text-color: #333;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
    Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  color: var(--text-color);
  background: var(--bg-color);
  min-height: 100vh;
}

.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 2rem;
  background: transparent;
  backdrop-filter: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  border-bottom: none;

  .logo {
    font-size: 1.5rem;
    font-weight: bold;
    text-decoration: none;
    color: inherit;
    background: linear-gradient(45deg, #007CF0, #00DFD8);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }

  .nav-right {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-left: auto;
    .user-area {
      min-width: 100px;
      display: flex;
      align-items: center;
      justify-content: flex-end;
    }
  }

  .user-info {
    display: flex;
    align-items: center;
    min-width: 80px;
    justify-content: flex-end;
    cursor: pointer;
    .avatar {
      width: 32px;
      height: 32px;
      border-radius: 50%;
      margin-right: 8px;
      background: #e0e0e0;
    }
    .username {
      font-size: 1rem;
      color: var(--text-color);
    }
  }

  .login-link {
    margin-right: 20px;
  }

  .login-btn {
    background: #007CF0;
    color: #fff;
    border: none;
    border-radius: 1.5rem;
    padding: 0.5rem 1.2rem;
    font-size: 1rem;
    cursor: pointer;
    transition: background 0.2s;
    min-width: 80px;
    &:hover {
      background: #005fa3;
    }
  }
}

/* 自定义下拉菜单样式 */
.custom-dropdown {
  border-radius: 8px;
  min-width: 120px;
  padding: 6px 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  background: white;
}

.logout-item {
  padding: 0 !important;
  margin: 0 !important;
  
  &:hover {
    background-color: transparent !important;
  }
}

.logout-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 10px 20px;
  background: transparent;
  border: none;
  font-size: 14px;
  color: #333;
  cursor: pointer;
  transition: all 0.2s ease;
  
  &:hover {
    color: #666666;
    background-color: #f9f9f9;
  }
  
  &:active {
    transform: scale(0.98);
    background-color: #f5f5f5;
  }

  .logout-icon {
    display: inline-flex;
    margin-right: 8px;
    color: #666;
  }
  
  &:hover .logout-icon {
    color: #666666;
  }
}

/* 去掉菜单项前面的点和默认样式 */
.el-dropdown-menu__item {
  list-style: none;
}

.el-dropdown-menu__item::before {
  display: none !important;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

@media (max-width: 768px) {
  .navbar {
    padding: 1rem;
  }
}
</style>
